﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;


public partial class admin_Product_listproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "Select")
        //{
        //    // Convert the row index stored in the CommandArgument
        //    // property to an Integer.
        //    int index = Convert.ToInt32(e.CommandArgument);

        //    // Retrieve the row that contains the button clicked 
        //    // by the user from the Rows collection.
        //    GridViewRow row = GridView1.Rows[index];

        //    // Create a new ListItem object for the contact in the row.     
           







           



            
        //    Label lbid = (Label)GridView1.Rows[index].FindControl("lbIDproduct");
        //    lbIDtype.Text = lbid.Text;
        //    txtName.Text = Server.HtmlDecode(row.Cells[1].Text);
        //    txtQuantity.Text = Server.HtmlDecode(row.Cells[3].Text);
        //    txtProduct_SaleOff.Text = Server.HtmlDecode(row.Cells[5].Text);
        //    Label lbprice = (Label)GridView1.Rows[index].FindControl("lbprice");
        //    txtPrice.Text = lbprice.Text;
        //    Label lbDetails = (Label)GridView1.Rows[index].FindControl("lbDetails");
        //    txtDetails.Text = lbDetails.Text;
        //    ddlType.SelectedValue = Server.HtmlDecode(row.Cells[8].Text);
        //    Image img = (Image)GridView1.Rows[index].FindControl("imgctproduct");

        //    imgProduct.ImageUrl = img.ImageUrl;

           
           
            
           

            
            
           
           
        //}
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Addproduct.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        //string str = "Update TheStorage Set ProductName='@name',Quantity='@quan',Price='@Price',Product_SaleOff='@prosale',Status='@s',Details='@Details',Image='@img',TypeProduct_ID='@typeProduct' Where ID_Product='@id' ";
        //SqlCommand cmd = new SqlCommand(str, conn);
        //cmd.Parameters.Add("s", lbIDtype.Text);
        //cmd.Parameters.Add("id", lbIDtype.Text);
        //cmd.Parameters.Add("@name", txtName.Text);
        //cmd.Parameters.Add("@quan", txtQuantity.Text);
        //cmd.Parameters.Add("@Price", txtPrice.Text);
        //cmd.Parameters.Add("@prosale", txtProduct_SaleOff.Text);
        //cmd.Parameters.Add("@Details", txtDetails.Text);
        //cmd.Parameters.Add("@typeProduct", ddlType.SelectedValue);
        //if (fuImage.HasFile)
        //{
        //    try
        //    {
        //        string filename = Path.GetFileName(fuImage.FileName);
        //        fuImage.SaveAs(Server.MapPath(".\\image\\") + filename);

        //        cmd.Parameters.Add("img", "~/admin/Product/image/" + filename);
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write("Lỗi" + ex);
        //    }
        //}



        //conn.Open();

        //cmd.ExecuteNonQuery();
        //GridView1.EditIndex = -1;
        //GridView1.DataBind();
        //GridView1.Visible = true;
        //Response.Write(lbIDtype.Text);
        //Response.Write(txtName.Text);
        //Response.Write(txtQuantity.Text);
        //Response.Write(txtPrice.Text);
        //Response.Write(txtProduct_SaleOff.Text);
        //Response.Write(txtDetails.Text);
        //Response.Write(ddlType.SelectedValue);
        //Response.Write(imgProduct.ImageUrl);
        //Response.Write(lbIDtype.Text);



        listproduct.Update();
       
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        txtName.Enabled = true;
        txtQuantity.Enabled = true;
        txtProduct_SaleOff.Enabled = true;
        txtPrice.Enabled = true;
        txtDetails.Enabled = true;
        fuImage.Visible = true;
        ddlType.Enabled = true;
        btnCancel.Visible = true;
        btnSave.Visible = true;
        btnUpdate.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        FormView1.PageIndex = GridView1.SelectedIndex;
    }
    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        string str = "update TheStorage set Image =@img where ID_Product =@id";
        SqlCommand cmd = new SqlCommand(str, conn);


        Label lbid = (Label)FormView1.FindControl("ID_ProductLabel1");
        cmd.Parameters.AddWithValue("@id", lbid.Text);
        FileUpload fileup = (FileUpload)FormView1.FindControl("FileUpload1");
        if (fileup.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fileup.FileName);
                fileup.SaveAs(Server.MapPath(".\\Image\\") + filename);
                Response.Write("Đã tải lên tệp " + " " + filename);
                cmd.Parameters.Add("@img", "~/admin/Product/image/" + filename);
            }
            catch (Exception ex)
            {
                Response.Write("Lỗi" + ex);
            }
        }
    }
}
